//
//  MapKitViewController.m
//  MyPizzaMobile
//
//  Created by Anton Atanasov on 19.02.13.
//  Copyright (c) 2013 г. Stanislav. All rights reserved.
//

#import "MapKitViewController.h"

@interface MapKitViewController ()
@property(readwrite) RequestManager * requestManager;
@end

@implementation MapKitViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.requestManager = [RequestManager requestManagerInstance];
}
- (void)viewWillAppear:(BOOL)animated
{
    UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonSystemItemFastForward target:self action:@selector(nextButtonPressed)];
    self.navigationItem.rightBarButtonItem = nextButton;
    
    CLLocationCoordinate2D centerCoordinate;
    centerCoordinate.latitude = 42.692176;//center(SU)
    centerCoordinate.longitude = 23.334745;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(centerCoordinate, 3000.0, 3000.0);
    MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
    [self.mapView setRegion:adjustedRegion animated:YES];
    [self setAnnotations];
}
- (void)setAnnotations
{
    NSString *title = @"ФМИ";
    NSString *subtitle = @"Вие сте тук";
    
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = 42.674359;//FMI
    coordinate.longitude = 23.330466;
    
    AnnotationView *annotation = [[AnnotationView alloc] initWithTitle:title Subtitle:subtitle coordinate:coordinate];
    [self.mapView addAnnotation:annotation];
    
    title = @"Пицария";
    subtitle = @"Вашата пица е тук";
    coordinate.latitude = 42.697858;//Pizzateria
    coordinate.longitude = 23.321145;
    
    annotation = [[AnnotationView alloc] initWithTitle:title Subtitle:subtitle coordinate:coordinate];
    [self.mapView addAnnotation:annotation];

}
- (IBAction)nextButtonPressed
{
    double totalPrice =0;
    for (NSString* key in self.requestManager.orderSupplements)
    {
        NSDictionary* temp =[self.requestManager.orderSupplements objectForKey:key];
        totalPrice = totalPrice + [[temp objectForKey:@"total"] doubleValue];
    }
    totalPrice += [[self.requestManager.orderPizzas objectForKey:@"size"] doubleValue]*0.3f;
    totalPrice += [self.requestManager getShippingPrice];
    [self.requestManager.orderPizzas setObject:[[NSString alloc] initWithFormat:@"%.2f",totalPrice] forKey:@"totalPrice"];
    NSString* alertMessage = [[NSString alloc]initWithFormat:@"Do you want to order pizza with size %@ cm. to address %@ for total price $%.2f",[self.requestManager.orderPizzas objectForKey:@"size"],[self.requestManager.orderPizzas objectForKey:@"orderAddress"],totalPrice];
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Order"
                                                      message:alertMessage
                                                     delegate:self
                                            cancelButtonTitle:@"Cancel"
                                            otherButtonTitles:@"OK", nil];
    message.tag = 1;
    [message show];
    //    [self performSegueWithIdentifier:@"chooseSupplements" sender:self];
}

- (void)displayMailComposerSheet {
	MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
	mailController.mailComposeDelegate = self;
	
	[mailController setSubject:@"Поръчах пица в ФМИ!"];
	
	// Fill out the email body text
	NSString *emailBody = @"Пицата ще дойде до 30мин... идвайте да ядем!";
	[mailController setMessageBody:emailBody isHTML:NO];
	
	[self presentViewController:mailController animated:YES completion:nil];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag ==1&&buttonIndex)
    {
        
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Send Mail or SMS"
                                                          message:@"Do you want to invite your friends to eat the ordered pizza?"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                                otherButtonTitles:@"Mail",@"SMS",nil];
        message.tag = 2;
        [message show];
        
    }
    if(alertView.tag ==2)
    {
        if(buttonIndex==1)
        {
            Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
            
            if (mailClass != nil && [mailClass canSendMail]) {
                [self displayMailComposerSheet];
            } else {
                NSLog(@"Device not configured to send mail.");
            }
        }else if (buttonIndex==2)
        {
            MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init] ;
            if([MFMessageComposeViewController canSendText])
            {
                controller.messageComposeDelegate = self;
                controller.body = @"Пицата ще дойде до 30мин... идвайте да ядем!";
                [self presentModalViewController:controller animated:YES];
            }
        }else
        {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissModalViewControllerAnimated:YES];
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    
	[self dismissModalViewControllerAnimated:YES];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setMapView:nil];
    [super viewDidUnload];
}
@end
